package com.laituo.juyou.app.api.groupshopuser;

import cn.hutool.json.JSONUtil;
import com.laituo.juyou.core.Const;
import com.laituo.juyou.data.component.CacheComponent;
import com.laituo.juyou.data.domain.GroupShopUserDTO;
import com.laituo.juyou.data.dto.goods.GroupShopDTO;
import com.laituo.juyou.data.mapper.GroupShopMapper;
import com.laituo.juyou.data.mapper.GroupShopUserMapper;
import com.laituo.juyou.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: rize
 * Date: 2019/11/24
 * Time: 15:26
 *
 * @author xuyang
 */
@Service
public class GroupShopUserServiceImpl implements GroupShopUserService {

    @Autowired
    private GroupShopUserMapper groupShopUserMapper;

    @Autowired
    private CacheComponent cacheComponent;

    /**
     *
     * @return
     * @throws ServerException
     */
    @Override
    public List<GroupShopUserDTO> getGroupShopUser() throws ServerException {
        Set<String> scan = cacheComponent.scan(Const.GROUP_USER_INFO + "*");
        Set<String> numScan = cacheComponent.scan(Const.GROUP_USER_NUM + "*");
        List<GroupShopUserDTO> collect = scan.stream().map(item -> {
            String raw = cacheComponent.getRaw(item);
            String numKey = item.replace(Const.GROUP_USER_INFO, Const.GROUP_USER_NUM);
            GroupShopUserDTO groupShopUserDTO = JSONUtil.toBean(raw, GroupShopUserDTO.class);
            groupShopUserDTO.setNum(cacheComponent.getRaw(numKey));
            return groupShopUserDTO;
        }).collect(Collectors.toList());
        return collect;
    }
}
